package com.atguigu.gmall.pay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

import java.util.HashMap;
import java.util.Map;

public class AlipayDemo {
    public static void main(String[] args) throws Exception{

        //创建Alipay客户端

        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",   //沙箱环境的url
                "2021000117644273",
                "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCV8LIltz4C6ij7ofiDVcupQqm5xU4B6knQRfznS3SJMnxmUGO8sQpLkv4XdAJvQmojJCx6wO4gZC/QhkRtZrlVX0Wo/OqUc2XggLjVftXOk8oYwTDeYKuSuNm8eeQ7lA+46zUDGz8nYOXc+OO5u/4sBwE0yNjtHtFMQHTIBZtaR3IvqRpGOoHrPcuX64e9fxPbEu2TIA7yXtJeUDXevaZq1Jh5EEfjiFBK2Z0QpaXLtO03uvyEceLRaexHzYFoM373FP1+XACwTQatBD5A3scWkfhi66z+ZFIOx/jLfM91P6sVYDpb/8ij1k/Dcr/5f6sCadiIIHpO3tDye+jg+/TJAgMBAAECggEBAIdEDELwK7ZPbseLmHlLRh8db6aS88vQg+SrrvsuXCSFciqPLVsF0EbbhfS8iJG8tamcMbBQKsL3/xa8ejDD8kCtYVeRS0K/VFITbivUb6gcNb7dUmvbhA0qS+u3fwmsif6F2l5CD1hEFzqhIjfl++fWLeVjqCdKnQCQSYpFXKA0285OADk8JJD/HKS+6evHSbFhe/MA8Bgdzr6AaNX6hPWgELxYBC0BblrK26WEtNkgwa9dToVkBTlshKbtuYp6hSj+Jwd4nUyTX6u79nQKCNEYFFUml3C74kwO/z3SUYwSs4TMc3/3f01k5FeegoU239aYfTmjdfG+4O75l9Yf9zECgYEA51yCxBV1tGrcCVCaq+o33sx/Lxeetosw4ikn4IEOyBnZc5/FwGT1Jti4TbE5mPv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fg9MX/br9QabSNw4piwLyH2/IBk4dZSx9ifiq7IuHtNxfR8LChebv0sS1uEJFLew4EYbVR7wknYJr3X5kgWX3Edri8YFXiHvSRq15OvKDKYyNJWFWlbzibC6aAAiT0rmJKf47yhC8BHacEBFzh65wIDAQAB",
                "RSA2");
        //上面七个参数是公共参数  每次请求都是需要带的参数

//-------------------------------------------------------------------------------------------
        //下面有四个参数是请求参数  是每次都需要带的参数
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();

        Map map = new HashMap();
        map.put("out_trade_no","sadasdasjwwrrrwtthdasdjassa123dasaa");
        map.put("product_code","FAST_INSTANT_TRADE_PAY");
        map.put("total_amount","15");
        map.put("subject","有钱也不花rffsqwwerfwe");

        request.setBizContent(JSONObject.toJSONString(map));
        AlipayTradePagePayResponse response = null;

            response = alipayClient.pageExecute(request);

/*<form name="punchout_form" method="post" action="https://openapi.alipaydev.com/gateway.do?charset=UTF-8&method=alipay.trade.page.pay&sign=fhxvRTsADDFvXHZaAUFyBodANluceOkLX7Ic8UUUnu2QduOOtbzeagt8sGynAQdXbX4wzSVXBQSfyEnC3Ve8kmvl1KicQa8tJqFqDi%2F4x%2BuVqa2nLRQFdkUkCE8NUG9TBeCDckNJsRrc4GRrMYpoTMliQS6cYNSzj3%2B8yWPe2fUPiB6VFILPx08bSpFSInGC8dHbbMBeJWlOOi0YEZ%2FsZnj4uuO9ihswP2XH6JT3VJAnA6VrE9vVOhUPLQLDuEJgbdPdwshnqfEZKft4jjYDJztU9RSv3iqPDBNqb4OPfeV2cJe79SPVXYMUZ41nDnyteCtrxNlM3OloWDaBkCAaCQ%3D%3D&version=1.0&app_id=2021000117644273&sign_type=RSA2&timestamp=2021-04-23+21%3A17%3A44&alipay_sdk=alipay-sdk-java-4.8.73.ALL&format=json">
<input type="hidden" name="biz_content" value="{&quot;out_trade_no&quot;:&quot;sadasdasjhdasdjas&quot;,&quot;total_amount&quot;:&quot;15&quot;,&quot;subject&quot;:&quot;有钱也不花,诶 就是玩&quot;,&quot;product_code&quot;:&quot;FAST_INSTANT_TRADE-PAY&quot;}">
<input type="submit" value="立即支付" style="display:none" >
</form>
<script>document.forms[0].submit();</script>
                调用成功*/

        if(response.isSuccess()){
            System.out.println(response.getBody());
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
